package com.das.mechanic_base.mvp.view.create.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.base.X3BaseFragment;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.bean.alone.AloneDelSelectedBean;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.mvp.view.create.SelectWorkActivity;
import com.das.mechanic_base.utils.LanguageConvent;
import com.das.mechanic_base.utils.X3PinyinComparator;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3WorkPinyinComparator;
import com.das.mechanic_base.widget.X3SlideBar;
import com.das.mechanic_base.widget.X3UserItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

public class X3ServiceListItemFragment extends X3BaseFragment implements X3AddServiceWorkAdapter.IOnSelectWorkList, X3SlideBar.onTouchLetterListener {

    @BindView(R2.id.rlv_item)
    RecyclerView rlv_item;
    @BindView(R2.id.sb_bar)
    X3SlideBar sb_bar;
    @BindView(R2.id.ll_sbar)
    LinearLayout ll_sbar;

    private SelectWorkBean.ShopCategoryEntityBean shopBean;
    private X3AddServiceWorkAdapter workAdapter;

    private X3WorkPinyinComparator pinyinComparator;
    private List<SelectWorkBean.ServiceBaseEntityBean> conventList = new ArrayList<>();
    private List<String> headerList;

    public boolean isDetection;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_service_list_item_layout;
    }

    public static X3ServiceListItemFragment newInstance(SelectWorkBean.ShopCategoryEntityBean workBean,boolean isDetection) {
        X3ServiceListItemFragment detectionFragment = new X3ServiceListItemFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("shopBean",workBean);
        bundle.putBoolean("isDetection",isDetection);
        detectionFragment.setArguments(bundle);
        return detectionFragment;
    }

    @Override
    protected void initView(View view) {
        shopBean = (SelectWorkBean.ShopCategoryEntityBean) getArguments().getSerializable("shopBean");
        isDetection = getArguments().getBoolean("isDetection");
        if (isDetection){
            ll_sbar.setVisibility(View.GONE);
        }else{
            ll_sbar.setVisibility(View.VISIBLE);
        }
        handleData(shopBean.serviceBaseEntityList);
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    private void handleData(List<SelectWorkBean.ServiceBaseEntityBean> serviceBaseEntityList){


        if (!isDetection){
            pinyinComparator = new X3WorkPinyinComparator();

            conventList.clear();
            conventList = LanguageConvent.sortWorkData(serviceBaseEntityList);
            /**
             * 对数据进行A-Z排序
             */
            Collections.sort(conventList, pinyinComparator);
            headerList = new ArrayList<>();
            for (SelectWorkBean.ServiceBaseEntityBean b : conventList) {
                headerList.add(b.getSortLetters());
            }
            Set<String> set = new LinkedHashSet<>(headerList);
            headerList = new ArrayList<>(set);

            if (sb_bar != null && !isDetection) {
                sb_bar.isSelectWork = true;
                sb_bar.changeNum(headerList);
            }
            rlv_item.addItemDecoration(new X3UserItemDecoration());
            workAdapter = new X3AddServiceWorkAdapter(getContext(),conventList,isDetection);
        }else{
            workAdapter = new X3AddServiceWorkAdapter(getContext(),serviceBaseEntityList,isDetection);
        }
        rlv_item.setLayoutManager(new LinearLayoutManager(getContext()));

        rlv_item.setAdapter(workAdapter);
        workAdapter.setiOnSelectWorkList(this);

        //获取当前的选择的项目
        if (getActivity() != null) {
            SelectWorkActivity activity = (SelectWorkActivity) getActivity();
            workAdapter.changeAlreadyAddService(activity.getServiceVariantBeanList());
        }

        if (!isDetection){
            sb_bar.setOnTouchLetterListener(this);
            rlv_item.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                    LinearLayoutManager layoutManager = (LinearLayoutManager) rlv_item.getLayoutManager();
                    int itemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                    if (itemPosition < 0 || itemPosition >= conventList.size()) {
                        return;
                    }
                    String sortLetters = conventList.get(itemPosition).getSortLetters();
                    if (X3StringUtils.isEmpty(sortLetters)) {
                        return;
                    }
                    for (int i = 0; i < headerList.size(); i++) {
                        if (sortLetters.equalsIgnoreCase(headerList.get(i))) {
                            if (sb_bar != null) {
                                sb_bar.changeSelectIndex(i);
                            }
                            break;
                        }
                    }
                }
            });
        }

    }

    @Override
    protected X3BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void iOnSelectWork(SelectWorkBean.ServiceBaseEntityBean serviceBean, boolean isAdd) {
        SelectWorkActivity workActivity = (SelectWorkActivity) getActivity();
        if (workActivity == null) {
            return;
        }
        workActivity.addWorkToStaff(serviceBean,isAdd);
    }

    @Override
    public void onTouchLetterChange(String letter, boolean touch) {
        for (int i = 0; i < conventList.size(); i++) {
            if (letter.equalsIgnoreCase(conventList.get(i).getSortLetters())) {
                ((LinearLayoutManager) rlv_item.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDelSelectedBean(AloneDelSelectedBean cameraBean) {
        //获取当前的选择的项目
        if (getActivity() != null) {
            SelectWorkActivity activity = (SelectWorkActivity) getActivity();
            workAdapter.changeAlreadyAddService(activity.getServiceVariantBeanList());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
